<template>
    <view class="uni-container">
        <uni-forms ref="form" v-model="formData" @validate="validate">
            <uni-forms-item name="namespace" label="命名空间名" required>
                <uni-data-select v-model="formData.namespace" :clear="false" :localdata="namespaceRange" @change="namespaceChange"></uni-data-select>
            </uni-forms-item>
            <uni-forms-item name="workloadName" label="工作负载名" required>
                <uni-easyinput v-model="formData.workloadName" :clearable="false" placeholder="请输入工作负载名" />
            </uni-forms-item>
            <uni-forms-item name="isRedeploy" label="是否重新部署" required>
                <switch @change="isRedeployChange($event.detail.value)" :checked="formData.isRedeploy" />
            </uni-forms-item>
            <uni-forms-item name="isNotify" label="是否发送通知" required>
                <switch @change="isNotifyChange($event.detail.value)" :checked="formData.isNotify" />
            </uni-forms-item>
            <view class="uni-button-group">
                <button style="width: 100px;" type="primary" class="uni-button" @click="validate">提交</button>
                <navigator style="margin-left: 15px;" url="./list">
                    <button style="width: 100px;" class="uni-button">返回</button>
                </navigator>
            </view>
        </uni-forms>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                formData: {
                    "namespace": "",
                    "workloadName": "",
                    "isRedeploy": false,
                    "isNotify": false
                },
                namespaceRange: []
            }
        },
        onLoad() {
            uni.request({
                url: '/auth/v1/confRancher/listNamespace',
                dataType: 'string',
                success: (res) => {
                    let data = res.data.data;
                    for (var i = 0; i < data.length; i++) {
                        this.namespaceRange[i] = {
                            "value": data[i],
                            "text": data[i]
                        }
                    }
                }
            });
        },
        methods: {
            /**
             * 表单提交
             */
            validate() {
                uni.showLoading({
                    title: '提交中...',
                    mask: true
                })

                uni.request({
                    method: 'POST',
                    url: '/auth/v1/workload/insert',
                    data: this.formData,
                    success: (res) => {
                        uni.hideLoading();
                        if (res.data.code == 200) {
                            uni.showToast({
                                title: '新增成功'
                            })
                        }
                    }
                });

                uni.hideLoading();
            },
            namespaceChange(e) {
                this.formData.namespace = e
            },
            isRedeployChange(e) {
                this.formData.isRedeploy = e
            },
            isNotifyChange(e) {
                this.formData.isNotify = e
            }
        }
    }
</script>
<style>
    ::v-deep .uni-forms-item__label {
        width: 130px !important;
    }
    
    .uni-easyinput{
        width: 800px;
    }
</style>
